package com.jsm.tf.net.message;

import com.google.common.collect.Maps;
import com.jsm.tf.net.message.request.*;
import com.jsm.tf.net.message.response.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.io.Serializable;
import java.util.HashMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public abstract class Message<T extends ResponseMessage> implements Serializable {
    public static final HashMap<Byte, Class<?>> commandClassMap = Maps.newHashMap();
    protected static final byte HEARTBEAT_COMMAND_TYPE = 0;
    protected static final byte LOGIN_COMMAND_TYPE = 1;
    protected static final byte LOGIN_RESPONSE_COMMAND_TYPE = 2;
    protected static final byte CREATE_ROOM_COMMAND_TYPE = 3;
    protected static final byte CREATE_ROOM_RESPONSE_COMMAND_TYPE = 4;
    protected static final byte LIST_ROOM_COMMAND_TYPE = 5;
    protected static final byte LIST_ROOM_RESPONSE_COMMAND_TYPE = 6;
    protected static final byte ROOM_CHAT_COMMAND_TYPE = 7;
    protected static final byte ROOM_CHAT_RESPONSE_COMMAND_TYPE = 8;
    protected static final byte JOIN_ROOM_COMMAND_TYPE = 9;
    protected static final byte JOIN_ROOM_RESPONSE_COMMAND_TYPE = 10;

    static {
        commandClassMap.put(HEARTBEAT_COMMAND_TYPE, HeartbeatMessage.class);
        commandClassMap.put(LOGIN_COMMAND_TYPE, LoginMessage.class);
        commandClassMap.put(LOGIN_RESPONSE_COMMAND_TYPE, LoginResponseMessage.class);
        commandClassMap.put(CREATE_ROOM_COMMAND_TYPE, CreateRoomMessage.class);
        commandClassMap.put(CREATE_ROOM_RESPONSE_COMMAND_TYPE, CreateRoomResponseMessage.class);
        commandClassMap.put(LIST_ROOM_COMMAND_TYPE, ListRoomMessage.class);
        commandClassMap.put(LIST_ROOM_RESPONSE_COMMAND_TYPE, ListRoomResponseMessage.class);
        commandClassMap.put(ROOM_CHAT_COMMAND_TYPE, RoomChatMessage.class);
        commandClassMap.put(ROOM_CHAT_RESPONSE_COMMAND_TYPE, RoomChatResponseMessage.class);
        commandClassMap.put(JOIN_ROOM_COMMAND_TYPE, JoinRoomMessage.class);
        commandClassMap.put(JOIN_ROOM_RESPONSE_COMMAND_TYPE, JoinRoomResponseMessage.class);
    }

    private String magic = "jsmtf";
    private byte version = 1;
    private byte serializeType = 3;
    private byte commandType;
    private long seq;

    public abstract byte getCommandType();
}
